﻿using Help;
using Model;
using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;

namespace BLLA
{
    public partial class Order_FileInfo_T : BaseBLL<Model.Order_FileInfo_T>, IBLL.IOrder_FileInfo_TBLL
    {
        ObjectSerialize os = new ObjectSerialize();
        DataResult result = new DataResult();

        /// <summary>
        /// 多图上传
        /// </summary>
        /// <param name="OperID"></param>
        /// <param name="json"></param>
        /// <param name="tag"></param>
        /// <param name="YZM"></param>
        /// <param name="RETID"></param>
        /// <returns></returns>
        public string UploadPic(int OperID, string json, int FileType, string YZM, int RETID)
        {
            #region 匹配验证码
            if (YZM != Help.ALL_DataV.YZM)
            {
                result.BS = "-88";
                result.Msg = "验证码错误！";
                return os.StringOfObject(result, RETID);
            }
            #endregion
            #region 执行操作
            try
            {
                int ret = -1;
                Model.Order_FileInfo_T info = ParseClass<Model.Order_FileInfo_T>.StrDeserialize(json, RETID);
                if (info != null)
                {
                    info.FileInfoID = GetSysKey.GetKey("FileInfoKey");
                    string time = DateTime.Now.ToString("yyyyMMddHHmmss");
                    info.CreateTime = time;
                    info.FileType = FileType;
                    //ret = base.Add(info);
                    bool boo = new Help.ComdSqlFactory().ExecuteScalar("INSERT INTO [dbo].[Order_FileInfo_T] ([FileInfoID],[FileURL],[DishesID],[FileName],[CreateTime],[FileType]) VALUES('" + info.FileInfoID + "','" + info.FileURL + "','" + info.DishesID + "','" + info.FileName + "','" + info.CreateTime + "'," + info.FileType + ");");
                    if (boo)
                    {
                        ret = 1;
                    }
                }
                if (ret > 0)
                {
                    // 将操作记录到日志表中
                    Model.Order_OperationFlow_T Log = new Model.Order_OperationFlow_T();
                    Log.OperationTitle = "图片上传";
                    Log.OperationText = "图片上传成功";
                    Log.OperationType = "";
                    Log.OperationRes = "成功";
                    Log.OperationUser = OperID;
                    string str_log = os.StringOfObject(Log, RETID);
                    bool isSuccess = new Order_OperationFlow_T().AddOrder_OperationFlow(str_log, RETID);
                    if (!isSuccess)
                    {
                        WriteLog.WriteW("新增系统操作流水失败");
                    }
                    result.BS = "1";
                    result.Msg = "上传成功";
                    result.rows = info;
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "上传失败";
                }
            }
            catch (DbEntityValidationException dbex)
            {
                StringBuilder errors = new StringBuilder();
                IEnumerable<DbEntityValidationResult> validationResult = dbex.EntityValidationErrors;
                foreach (DbEntityValidationResult result1 in validationResult)
                {
                    ICollection<DbValidationError> validationError = result1.ValidationErrors;
                    foreach (DbValidationError err in validationError)
                    {
                        errors.Append(err.PropertyName + ":" + err.ErrorMessage + "\r\n");
                    }
                }
                //Console.WriteLine(errors.ToString());
                WriteLog.WriteW("图片上传异常：" + errors.ToString());
                result.BS = "-99";
                result.Msg = "系统异常1";
            }
            catch (DbUpdateException ex)
            {
                WriteLog.WriteW("图片上传DbUpdateException：" + ex.Message);
                WriteLog.WriteW(" 图片上传   " + ex.InnerException.Message);
                result.BS = "-99";
                result.Msg = "系统异常2";
            }
            catch (Exception ex)
            {
                WriteLog.WriteW("图片上传：" + ex.Message);
                result.BS = "-99";
                result.Msg = "系统异常3";
            }
            #endregion
            return os.StringOfObject(result, RETID);
        }

        /// <summary>
        /// 根据DishesID和类型获取商品对应的图片
        /// </summary>
        /// <param name="DishesID"></param>
        /// <param name="FileType"></param>
        /// <param name="YZM"></param>
        /// <param name="RETID"></param>
        /// <returns></returns>
        public string GetOrder_DishesPhoto(string DishesID,int FileType, string YZM, int RETID)
        {
            try
            {
                #region 匹配验证码
                if (YZM != Help.ALL_DataV.YZM)
                {
                    result.BS = "-88";
                    result.Msg = "验证码错误！";
                    return os.StringOfObject(result, RETID);
                }
                #endregion
                List<Model.Order_FileInfo_T> ls = new List<Model.Order_FileInfo_T>();
                ls = base.GetListBy(u => u.DishesID == DishesID && u.FileType == FileType);
                var sut = from m in ls
                          select new
                          {
                              m.FileInfoID,
                              m.FileURL,
                              m.DishesID,
                              m.FileName,
                              m.CreateTime,
                              m.FileType
                          };
                if (sut != null & sut.Count() > 0)
                {
                    result.BS = "1";
                    result.Msg = "查询成功";
                    result.rows = sut;
                    result.total = ls.Count;
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "查询不到数据";
                }
                return os.StringOfObject(result, RETID);
            }
            catch (Exception ex)
            {
                WriteLog.WriteW("根据主表ID获取商品图片信息列表失败：" + ex.Message);
                result.BS = "-99";
                result.Msg = "系统错误！";
                return os.StringOfObject(result, RETID);
            }
        }

        /// <summary>
        /// 图片删除
        /// </summary>
        /// <param name="OperID"></param>
        /// <param name="json"></param>
        /// <param name="RETID"></param>
        /// <returns></returns>
        public string DishesPhoto_Del(int OperID, string json, int RETID)
        {
            try
            {
                int ret = 0;
                string FileURL = "";
                List<Model.Order_FileInfo_T> list_tmp = base.GetListBy(u => u.FileInfoID == json);
                if (list_tmp != null && list_tmp.Count > 0)
                {
                    Model.Order_FileInfo_T tmp = list_tmp.First();
                    //ret = base.Del(tmp);
                    bool bo = new ComdSqlFactory().ExecuteScalar("DELETE FROM [dbo].[Order_FileInfo_T] WHERE [FileInfoID]='" + json + "';");
                    if (bo)
                    {
                        FileURL = tmp.FileURL;
                        ret = 1;
                    }
                }
                if (ret > 0)
                {
                    // 将操作记录到日志表中
                    Model.Order_OperationFlow_T Log = new Model.Order_OperationFlow_T();
                    Log.OperationTitle = "删除商品图片信息";
                    Log.OperationText = "删除商品图片成功";
                    Log.OperationType = "";
                    Log.OperationRes = "成功";
                    Log.OperationUser = OperID;
                    string str_log = os.StringOfObject(Log, RETID);
                    bool isSuccess = new Order_OperationFlow_T().AddOrder_OperationFlow(str_log, RETID);
                    if (!isSuccess)
                    {
                        WriteLog.WriteW("新增系统操作流水失败");
                    }
                    result.BS = "1";
                    result.Msg = "删除成功";
                    result.rows = FileURL;
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "删除失败";
                }
            }
            catch (Exception ex)
            {
                WriteLog.WriteW("删除商品图片信息：" + ex.Message);
                result.BS = "-99";
                result.Msg = "系统异常";
            }

            return os.StringOfObject(result, RETID);
        }

        /// <summary>
        /// 根据DishesID获取商品对应的所有图片
        /// </summary>
        /// <param name="DishesID"></param>
        /// <param name="YZM"></param>
        /// <param name="RETID"></param>
        /// <returns></returns>
        public string GetOrder_DishesPhotoByDishesID(string DishesID, string YZM, int RETID)
        {
            try
            {
                #region 匹配验证码
                if (YZM != Help.ALL_DataV.YZM)
                {
                    result.BS = "-88";
                    result.Msg = "验证码错误！";
                    return os.StringOfObject(result, RETID);
                }
                #endregion
                List<Model.Order_FileInfo_T> ls = new List<Model.Order_FileInfo_T>();
                ls = base.GetListBy(u => u.DishesID == DishesID);
                var sut = from m in ls
                          select new
                          {
                              m.FileInfoID,
                              m.FileURL,
                              m.DishesID,
                              m.FileName,
                              m.CreateTime,
                              m.FileType
                          };
                if (sut != null & sut.Count() > 0)
                {
                    result.BS = "1";
                    result.Msg = "查询成功";
                    result.rows = sut;
                    result.total = ls.Count;
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "查询不到数据";
                }
                return os.StringOfObject(result, RETID);
            }
            catch (Exception ex)
            {
                WriteLog.WriteW("根据ID和类型获取商品对应的图片信息列表失败：" + ex.Message);
                result.BS = "-99";
                result.Msg = "系统错误！";
                return os.StringOfObject(result, RETID);
            }
        }

    }
}
